package com.example.alibabanacosdiscoveryclient01.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.alibabanacosdiscoveryclient01.service.ConsumerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @createBy guoqiang
 * @createTime 2019/4/3
 * @describe
 */
@RestController
@Slf4j
@RequestMapping("/client")
@Tag(name="服务消费01")
public class ConsumerController {
    @Autowired
    ConsumerService consumerService;

    @SentinelResource(value = "black",fallback = "myFallback")
    @GetMapping("/black")
    @Operation(summary ="服务消费01-黑名单")
    public String black(String origin) {
        return "我是消费端01的黑名单方法，接口是通的,来源:"+origin;
    }

    @SentinelResource(value = "white")
    @GetMapping("/white")
    @Operation(summary ="服务消费01-白名单")
    public String white(String origin) {
        return "我是消费端01白名单的方法，接口是通的,来源:"+origin;
    }

    /*如果同时设置了 blockHandler限流的异常和fallback降级的处理，只会走blockHandler指定的方法*/
    @SentinelResource(value = "hello",/*blockHandler = "myBlockExceptionHandler",*/fallback = "myFallback")
    @GetMapping("/hello")
    @Operation(summary ="服务消费01-测试调取服务端的方法")
    public String hello(String name) {
        log.info("我是消费端01的接口，远程调用服务端接口是通的，参数是:" + name);
        return consumerService.hello(name);
    }
    @GetMapping("/test")
    @Operation(summary ="服务消费-test")
    public String test(String name) throws Exception {
          return "name is :"+name;
//        return "测试gateway配置的熔断器，我是消费端的接口，远程调用服务端接口是通的，参数是:" + name;
    }
   public String myBlockExceptionHandler(String name, BlockException blockException){
        blockException.printStackTrace();
        return "我是限流的异常处理01";
   }
    public String myFallback(String name){
        return "我是降级的异常处理01";
    }
    /*
    sentinel自定义资源限流规则
    自定义限流规则就不是添加方法的访问路径，配置的是@SentinelResouce注解中value的值
    比如@SentinelResource（"testresource"）就是配置路径为testresource
    * */
//    @SentinelResource(value = "testresource",blockHandler = "handleException",blockHandlerClass = {ExceptionUtil.class})
//        @RequestMapping("/testresource")
//    public String testresource(){
//        return  "我是client端的testresource接口，Sentinel自定义资源限流testresource";
//    }
    /*
      sentinel降级规则
      自定义降级规则就不是添加方法的访问路径，配置的是@SentinelResouce注解中value的值
      比如@SentinelResource（"hello"）就是配置路径为hello
       */
//    @SentinelResource(value = "hello",fallback ="handleFallback")
//    @RequestMapping("/hello2")
//    public String hello(String name){
//        log.info("我是client端的sentinel降级规则接口");
//        name=name+":我是client端的sentinel降级规则接口";
//        return  helloRemote.hello(name);
//    }
//    public static String handleFallback(String name){
//        return  "Sentinel 熔断降级处理fallback:"+name;
//    }
}
